<?php

class Data_export extends Controller{
  
      function data_export()
      {
          parent::Controller();
          $this->load->helper(array('url','form'));
          $this->load->library(array('form_validation','session'));
          $this->load->model('readbatchcard_model');
          //this is to update batchdetails of completed records...
          //$this->readbatchcard_model->update_batchdetails();  
      }
      
      function show_menu()
      {
         $displayform = '';
         $user_ses = $this->session->userdata('userlogindetails');
         
                  
        if($user_ses['user_id'] && ($user_ses['user_group'] == 1))
       {
          
        $displayform .='<div id=dashboard ><span>Data Export</span> <a href='.base_url().'/idea_home/admin_dashboard ><img align=right src="'.base_url().'images/home.png" alt="home" /></a>&nbsp;<img align=right src="'.base_url().'images/back.png" id="back"" alt="back" ONCLICK="history.go(-1)" /></div>';
        $displayform .= '<span style="float:left;">Welcome '.$user_ses['user_name'].'</span><div style="float:right;"><a href="'.base_url().'/">&nbsp;<a href="'.base_url().'add_xls">Data Import</a>&nbsp;<a href="'.base_url().'data_export/show_menu">Data Export</a>&nbsp;<a href="'.base_url().'idea_home/show_users">Show Users</a>&nbsp;<div id=logout><a href="'.base_url().'idea_home/logout">Logout</a></div></div>';
        $displayform .= '<div id=space>';
        //$displayform .= '<B>Please Select your way to Export Data</B>';
        $displayform .='<div id=status>';
        $displayform .='<fieldset><legend>Menu</legend>';
        $displayform .= '<span id="by_date" style="float:left;color:blue;cursor:pointer;">&nbsp;By Recieved Date</span><br/><br/>';
        $displayform .= '<span id="by_batch" style="float:left;color:blue;cursor:pointer;" >&nbsp;By Batch Numbers</span>';
        $displayform .='</fieldset></div>';
        $displayform .='<div id="batchcard_details">';
        
       
        
        $displayform .='</div>';
        
        $displayform .='</div>';
        $displayform .='<div id="show_batch"></div>';
        
        $this->readbatchcard_model->update_batchdetails();
             
             $displayform .= '<br/>';
             
             $data = array('formdisplay'=>$displayform);
             
             $this->template->add_js('
             
              $(document).ready(function(){
                  
                $("#by_date").click(function(){
                  
                    $.post("/data_export/by_date", 
                          function(data){
                            
                                     $("#batchcard_details").html("<br/>"+data);
                                     $("#show_batch").html(" ");
                                    }, "html");
                  
                });
                
                $("#by_batch").click(function(){
                  
                      
                  $.post("/data_export/by_batch", 
                          function(data){
                            
                                    $("#batchcard_details").html("<br/>"+data);
                                    $("#show_batch").html(" ");
                                    }, "html");
                  
                  
                });
              });
             
             ','embed');
             
        
              $this->template->write_view('header','header', $data, True);
              $this->template->write_view('content', 'other_general');
              $this->template->render();
        }
        else
        {
          echo "Forbidden to access!!!";
        }
      }
      
      function by_date()
      {
        $dispAjax ='';
        $dispAjax .='<div>';
        $dispAjax .= '<span style="color:blue;float:left;">By Received Date</span><br/>';
        $dispAjax .='From:<span><input type=text class="datepicker" size="8" id="start_date" />&nbsp;';
        $dispAjax .='To:<span><input type=text class="datepicker" size="8" id="end_date" />';
        $dispAjax .='<input type="button" id="get_batch" Value="Get Batch" />';
        $dispAjax .='</div>';
        
        $dispAjax .='<script type="text/javascript" src="'.base_url().'js/ui/jquery.ui.datepicker.js" ></script>';
        $dispAjax .='<script>';
        $dispAjax .='
          $(document).ready(function(){
                $(function() {
                           $( ".datepicker" ).datepicker({dateFormat: "dd/mm/yy"});
                          
                             });
                             
                $("#get_batch").click(function(){
                      var start =$("#start_date").val(),
                      end = $("#end_date").val();
                      
                        if(start == "" || end == "")
                        {
                          alert("please select Date"); 
                        }
                        else
                        {
                         $.post("/data_export/get_batch_by_date", {start:start,end:end},
                          function(data){
                            
                                    $("#show_batch").html("<br/>"+data);
                                    
                                    }, "html");
                        }
                });
          });   
        ';
        $dispAjax .='</script>';
        
        
        
        echo $dispAjax;
      }
      
      function by_batch()
      {
          $dispAjax ='';
        $dispAjax .='<div>';
        $dispAjax .= '<span style="color:blue;float:left;">By Batch</span><br/>';
   //     $dispAjax .='<fieldset><legend></legend>'
        $dispAjax .='From:<span><input type=text class="datepicker" size="8" id="start_date" />&nbsp;';
        $dispAjax .='To:<span><input type=text class="datepicker" size="8" id="end_date" />';
        $dispAjax .='<input type="button" id="get_batch" Value="Get Batch" />';
        $dispAjax .='</div>';
        
        $dispAjax .='<script type="text/javascript" src="'.base_url().'js/ui/jquery.ui.datepicker.js" ></script>';
        $dispAjax .='<script>';
        $dispAjax .='
          $(document).ready(function(){
                $(function() {
                           $( ".datepicker" ).datepicker({dateFormat: "dd/mm/yy"});
                          
                             });
                             
                $("#get_batch").click(function(){
                      var start =$("#start_date").val(),
                      end = $("#end_date").val();
                      
                        if(start == "" || end == "")
                        {
                          alert("please select Date"); 
                        }
                        else
                        {
                         $.post("/data_export/get_batch_all", {start:start,end:end},
                          function(data){
                            
                                    $("#show_batch").html("<br/>"+data);
                                    
                                    }, "html");
                        }
                });
          });   
        ';
        $dispAjax .='</script>';
        
        
        
        echo $dispAjax;
      }
      
     
    //showing only batch numbers which is left in selectd date..  
    function get_batch_by_date()
    {
      
     $res = $this->readbatchcard_model->get_batchbydate();
     $result = $res->result_array();
     
     $disp_batch = '';
       if($result != null)
       {
             $disp_batch .='<form id="form" action="'.base_url().'data_export/generate_csv" method="post">';
			 $i=1;
           foreach($result as $row)
           {
             
              $disp_batch .= '<span style="float:center;color:#999999"><input type="checkbox" name='.$i.' id='.$row['batchnum_id'].' value='.$row['batch_num'].' checked=checked />'.$row['batch_num'].'</span><br/>';
			  $i++;
             //print_r($row);  
           }
		$disp_batch .= '<span style="float:center;"><input type="submit" value="Generate" /></span>';
              $disp_batch .='</form>';
             
              $disp_batch .= '<script>';
            $disp_batch .='
                  $(document).ready(function(){
                    
                    $("#generate_csv").click(function(){
                      
                      var details = $("form").serialize();
                      $.post("/data_export/generate_csv", {batch_no:details},
                          function(data){
                            
                                    //$("#show_batch").html("<br/>"+data);
                                    
                                    }, "html");
                      
                    });
                    
                  });
            ';
            $disp_batch .= '</script>';
         
        } 
        else
        {
          $disp_batch .= '<span style="float:center;color:#999999;"> No Batch Numbers are ready to process!!</span>';
        }
  
       
     echo $disp_batch;
    }
    
    //displaying all batch numbers in selected date period... 
    function get_batch_all()
    {
     $res = $this->readbatchcard_model->get_batchall();
     $result = $res->result_array();
     
     $disp_batch = '';
       if($result != null)
       {
            $disp_batch .='<form id="form" action="'.base_url().'data_export/generate_csv" method="post" >';
			$i=1;
           foreach($result as $row)
           {
             
             $disp_batch .= '<span style="float:center;color:#999999"><input type="checkbox" name='.$i.' id='.$row['batchnum_id'].' value='.$row['batch_num'].' />'.$row['batch_num'].'</span><br/>';
			 $i++;
             //print_r($row);  
           }
             $disp_batch .= '<span style="float:center;"><input type="submit" value="Generate" /></span>';
             $disp_batch .='</form>';
             $disp_batch .= '<script>';
             
            $disp_batch .='
                  $(document).ready(function(){
                    
                    $("#generate_csv").click(function(){
                      
                      var details = $("form").serialize();
                      $.post("/data_export/generate_csv", {batch_no:details},
                          function(data){
                            
                                                                        
                                    }, "html");
                      
                    });
                    
                  });
            ';
            $disp_batch .= '</script>';
         
        } 
        else
        {
          $disp_batch .= '<span style="float:center;color:#999999;"> No Batch Numbers are ready to process!!</span>';
        }
  
       
     echo $disp_batch;
    }
    
    function generate_csv()
    {
	    
		  //testing csv file generation here...
		
		   header("Content-type: application/csv");
			header("Content-Disposition: attachment; filename=file.csv");
            header("Pragma: no-cache");
            header("Expires: 0");
		   	$this->readbatchcard_model->generate_csv();
	  
    }

    //this function is to generate csv file....
    //url to refer for more information later...
    //http://maestric.com/doc/php/codeigniter_csv
  
    
       //checking for duliplicate file while uploading excel sheet.....
    function check_file()
    {
      
      $val = $this->readbatchcard_model->check_dup_file();
    // $exist = $val->result_array();
    // print_r($exist); 
       
    }
  
  
   
}
